import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Head from "../components/Head";
import Login from "../components/Login";
import Register from "../components/Register";
import Layout from "../components/Layout";
import Index from "../components/Index";
import Category from "../components/Category";
import CategoryList from "../components/CategoryList";
import Tag from "../components/Tag"
import Archive from "../components/Archive";
import Write from "../components/Write";
import Interaction from "../components/Interaction";
import Detail from "../components/Detail";
import AdminLayout from "../components/admin/AdminLayout";
import AdminType from "../components/admin/AdminType";
import AdminUser from "../components/admin/AdminUser";
import AdminBlog from "../components/admin/AdminBlog";
import AdminTag from "../components/admin/AdminTag";
import el from "element-ui/src/locale/lang/el";
import jwtDecode from 'jwt-decode'

Vue.use(VueRouter)

import {Message} from 'element-ui';
import Edit from "../components/Edit";
import Welcome from "../components/admin/Welcome";
import AdminCustom from "../components/admin/AdminCustom";
import AdminSwiper from "../components/admin/AdminSwiper";
import AdminLink from "../components/admin/AdminLink";
import TagList from "../components/TagList";
import AdminComment from "../components/admin/AdminComment";
import Love from "../components/Love";
import Platform from "../components/Platform";
import PlatFormEdit from "../components/PlatFormEdit";
import AdminIndex from "../components/admin/AdminIndex";
import Collection from "../components/Collection";
// import jwtDecode from "jwt-decode";
// Vue.prototype.$message = Message;
const routes = [

    {
        path: '/platform',
        name: 'Platform',
        component: Platform,

    },
    {
        path: '/platformedit',
        name: 'PlatformEdit',
        component: PlatFormEdit,

    }
    ,
    // 二次路由 前台
    {
        path: '/edit',
        name: 'Edit',
        component: Edit,
        meta: {
            requireAuth: true
        },
    },
    {
        path: '/write',
        name: 'Write',
        component: Write,
        // meta: {
        //     requireAuth: true
        // },
    }
    ,
    {
        path: '/',
        name: 'Layout',
        redirect: '/index',
        component: Layout,
        children: [
            {
                path: '/index',
                name: 'Index',
                component: Index
            },
            {
                path: '/category',
                name: 'Category',
                component: Category
            },
            {
                path: '/categorylist',   // 配置对应的路由
                name: 'CategoryList',
                component: CategoryList,
                // redircet:

            },
            {
                path: '/taglist',   // 配置对应的路由
                name: 'TagList',
                component: TagList,
                // redircet:

            },
            {
                path: '/tag',
                name: 'Tag',
                component: Tag
            },
            {
                path: '/archive',
                name: 'Archive',
                component: Archive
            },
            {
                path: '/interaction',
                name: 'Interaction',
                component: Interaction
            },
            {
                path: '/detail',
                name: 'Detail',
                component: Detail
            },
            {
                path: '/love',
                name: 'Love',
                component: Love,

            },
            {
                path: '/collection',
                name: 'Collection',
                component: Collection,

            },
        ]
    }
    ,
    {
        path: '/register',
        name: 'Register',
        component: Register
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    // 前台 路由结束
    // 后台路由开始
    {
        path: '/admin',
        name: 'AdminLayout',   // 头部、左侧框架
        component: AdminLayout,
        redirect: '/adminindex',
        meta: {
            requireAuth: true  // 说明要进行验证
        },
        children: [
            {
                path: '/adminindex',
                name: 'AdminIndxe',
                component: AdminIndex,
                meta: {
                    requireAuth: true
                },
            },

            {
                path: '/welcome',
                name: 'Welcome',
                component: Welcome,
                meta: {
                    requireAuth: true
                },
            },
            {
                path: '/admintype',
                name: 'AdminType',
                component: AdminType,
                meta: {
                    requireAuth: true
                },
            },
            {
                path: '/adminuser',
                name: 'AdminUsere',
                component: AdminUser,
                meta: {
                    requireAuth: true
                },
            },
            {
                path: '/adminblog',
                name: 'AdminBlog',
                component: AdminBlog,
                meta: {
                    requireAuth: true
                },
            }, {
                path: '/admintag',
                name: 'AdminTag',
                component: AdminTag,
                meta: {
                    requireAuth: true
                },

            }, {
                path: '/admincustom',
                name: 'AdminCustom',
                component: AdminCustom,
                meta: {
                    requireAuth: true
                },

            }, {
                path: '/adminswiper',
                name: 'AdminSwiper',
                component: AdminSwiper,
                meta: {
                    requireAuth: true
                },

            },
            {
                path: '/adminlink',
                name: 'AdminLink',
                component: AdminLink,
                meta: {
                    requireAuth: true
                },

            },
            {
                path: '/admincomment',
                name: 'AdminComment',
                component: AdminComment,
                meta: {
                    requireAuth: true
                },

            },


        ]
    },
    // 后台路由结束
    {
        path: '/head',
        name: 'Head',
        component: Head
    },
    {
        path: '/about',
        name: 'About',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
    },

]

const router = new VueRouter({
    routes
})
router.beforeEach((to, from, next) => {
    console.log(to)
    // console.log(this)
    if (to.meta.requireAuth) { // 判断路由是否需要权限
        if (localStorage.getItem('token')) { // 如果有token直接放
            const token = localStorage.getItem('token')
            let type = localStorage.getItem('type')
            // console.log(type)
            if (to.name === 'Edit') {
                // next({
                //     path: '/edit'
                // })
                // return;
            }
            if (type != 1) {
                Message.error('游客没有权限！')
                next({
                    path: '/login',
                    query: {redirect: to.path}
                })
            } else {
                next();

            }
            // let path = to.name
            // let i = path.lastIndexOf('Admin')
            // if (i > 0) {    // 说明要访问后台页面
            //
            // }
        } else {// 如果没有token，又想访问就跳转到登录页
            Message.error("请先登录！")
            next({
                path: '/login',
                query: {redirect: to.path}
            })
        }
    } else {  // 访问的是没有权限的页面
        // if (to.path === '/login') {  // 如果有token，就是登录过了，就不需要再登录
        //     Message.info('您已登录')
        //     next({
        //         path: '/index'
        //     })
        // }
        next();
    }
    // if (localStorage.getItem('token') || to.name == 'Login' || to.name == 'Register') {
    //     next();
    // } else {
    //     next({
    //         path: '/'
    //     })
    // }
})
export default router
